package net.huashitong.node.dao;

import java.util.List;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.filter.NodeNewtempletFilter;
import org.springframework.stereotype.Repository;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;

import net.huashitong.node.entity.NodeNewtemplet;

@Repository
public class NodeNewtempletDao extends StringDao<NodeNewtemplet> {
    public List<NodeNewtemplet> queryNodeNewtempletByNodeType(String nodeType) {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.nodeType = :nodeType").setParam("nodeType", nodeType);
        return query.listResult();
    }

    /**
     * 条件查询
     *
     * @param filter
     * @return
     */
    public List<NodeNewtemplet> getNodeTempletInfo(NodeNewtempletFilter filter) {
        GenericQuery query = createQuery("obj");
        if (filter.getNodeId() != null && !"".equals(filter.getNodeId()))
            query.append(" and obj.nodeId =:nodeId").setParam("nodeId", filter.getNodeId());
        if (filter.getNodeName() != null && !"".equals(filter.getNodeName()))
            query.append(" and obj.nodeName =:nodeName").setParam("nodeName", filter.getNodeName());
        if (filter.getResDept() != null && !"".equals(filter.getResDept()))
            query.append(" and obj.resDept =:resDept").setParam("resDept", filter.getResDept());
        if (filter.getResUser() != null && !"".equals(filter.getResUser()))
            query.append(" and obj.resUser =:resUser").setParam("resUser", filter.getResUser());
        if (filter.getOperatorUser() != null && !"".equals(filter.getOperatorUser()))
            query.append(" and obj.operatorUser =:operatorUser").setParam("operatorUser", filter.getOperatorUser());
        if (filter.getType() != null && !"".equals(filter.getType()))
            query.append(" and obj.type =:type").setParam("type", filter.getType());
        if (filter.getPlanTime() != null && !"".equals(filter.getPlanTime()))
            query.append(" and obj.planTime =:planTime").setParam("planTime", filter.getPlanTime());
        if (filter.getFactTime() != null && !"".equals(filter.getFactTime()))
            query.append(" and obj.factTime =:factTime").setParam("factTime", filter.getFactTime());
        if (filter.getPlanBeginDate() != null && !"".equals(filter.getPlanBeginDate()))
            query.append(" and obj.planBeginDate =:planBeginDate").setParam("planBeginDate", filter.getPlanBeginDate());
        if (filter.getFactFinishDate() != null && !"".equals(filter.getFactFinishDate()))
            query.append(" and obj.factFinishDate =:factFinishDate").setParam("factFinishDate", filter.getFactFinishDate());
        if (filter.getStatus() != null && !"".equals(filter.getStatus()))
            query.append(" and obj.status =:status").setParam("status", filter.getStatus());
        if (filter.getNodeType() != null && !"".equals(filter.getNodeType())) {
            query.append(" and obj.nodeType =:nodeType").setParam("nodeType", filter.getNodeType());
        }
        if (filter.getPaging() == null) {
            query.append(" ORDER BY obj.nodeId+0 ASC");
        }
        return query.listResult();
    }


    /**
     * 条件查询
     *
     * @param filter
     * @return
     */
    public List<NodeNewtemplet> getNodeTempletInfo1(NodeNewtempletFilter filter) {
        GenericQuery query = createQuery("obj");
        if (filter.getNodeType() != null && !"".equals(filter.getNodeType())) {
            query.append(" and obj.nodeType =:nodeType").setParam("nodeType", filter.getNodeType());
        }
        query.append(" ORDER BY obj.nodeId+0 DESC");
        return query.listResult();
    }


    /**
     * 查询部门人员对应节点
     *
     * @param filter
     * @return
     */
    public List<NodeNewtemplet> getNodeForAcc(NodeNewtempletFilter filter) {
        GenericQuery query = createQuery("obj");
        if (filter.getNodeId() != null && !"".equals(filter.getNodeId()))
            query.append(" and obj.nodeId =:nodeId").setParam("nodeId", filter.getNodeId());
        if (filter.getNodeName() != null && !"".equals(filter.getNodeName()))
            query.append(" and obj.nodeName =:nodeName").setParam("nodeName", filter.getNodeName());
        if (filter.getResDept() != null && !"".equals(filter.getResDept()))
            query.append(" and obj.resDept like:resDept").setParam("resDept", "%" + filter.getResDept() + "%");
        if (filter.getResUser() != null && !"".equals(filter.getResUser()))
            query.append(" and obj.resUser =:resUser").setParam("resUser", filter.getResUser());
        if (filter.getOperatorUser() != null && !"".equals(filter.getOperatorUser()))
            query.append(" and obj.operatorUser =:operatorUser").setParam("operatorUser", filter.getOperatorUser());
        if (filter.getType() != null && !"".equals(filter.getType()))
            query.append(" and obj.type =:type").setParam("type", filter.getType());
        if (filter.getPlanTime() != null && !"".equals(filter.getPlanTime()))
            query.append(" and obj.planTime =:planTime").setParam("planTime", filter.getPlanTime());
        if (filter.getFactTime() != null && !"".equals(filter.getFactTime()))
            query.append(" and obj.factTime =:factTime").setParam("factTime", filter.getFactTime());
        if (filter.getPlanBeginDate() != null && !"".equals(filter.getPlanBeginDate()))
            query.append(" and obj.planBeginDate =:planBeginDate").setParam("planBeginDate", filter.getPlanBeginDate());
        if (filter.getFactFinishDate() != null && !"".equals(filter.getFactFinishDate()))
            query.append(" and obj.factFinishDate =:factFinishDate").setParam("factFinishDate", filter.getFactFinishDate());
        if (filter.getStatus() != null && !"".equals(filter.getStatus()))
            query.append(" and obj.status =:status").setParam("status", filter.getStatus());
        if (filter.getSysId() != null && !"".equals(filter.getSysId()))
            query.append(" and obj.sysId =:sysId").setParam("sysId", filter.getSysId());
        if (filter.getNodeType() != null && !"".equals(filter.getNodeType())) {
            query.append(" and obj.nodeType =:nodeType").setParam("nodeType", filter.getNodeType());
        }
        if (filter.getNodeIds() != null && !"".equals(filter.getNodeIds())) {
            query.append(" and obj.nodeIds =:nodeIds").setParam("nodeIds", filter.getNodeIds());
        }
        query.append("  GROUP BY node_id");
        if (filter.getPaging() == null) {
            query.append(" ORDER BY obj.nodeId+0 ASC");
        }
        return query.listResult();
    }

    public List<NodeNewtemplet> queryByNodeFilter(String nodeFilterId) {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.nodeId =:nodeId").setParam("nodeId", nodeFilterId);
        query.append(" and nodeFilter is not null");
        return query.listResult();
    }

    public NodeNewtemplet queryNodeNewTempletByNode(NodeNewtempletFilter filter){
        GenericQuery query = createQuery("obj");
        if(ObjectUtils.isNotBlank(filter.getNodeId())){
            query.append(" and obj.nodeId =:nodeId").setParam("nodeId", filter.getNodeId());
        }
        if(ObjectUtils.isNotBlank(filter.getNodeIds())){
            query.append(" and obj.nodeIds =:nodeIds").setParam("nodeIds", filter.getNodeIds());
        }
        return query.firstResult();
    }

    /**
     * 根据经办人名字手机号查询节点模板
     * @param phone
     * @param name
     * @return
     */
    public List<NodeNewtemplet> queryOperator(String phone, String name) {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.operatorUser = :operatorUser ").setParam("operatorUser", name);
        query.append(" and obj.operatorPhone = :operatorPhone order By obj.nodeId desc ").setParam("operatorPhone", phone);
        return query.listResult();
    }

    /**
     * 根据责任人名字手机号查询节点模板
     * @param phone
     * @param name
     * @return
     */
    public List<NodeNewtemplet> queryResUser(String phone, String name) {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.resUser = :resUser ").setParam("resUser", name);
        query.append(" and obj.resUserPhone = :resUserPhone order By obj.nodeId desc ").setParam("resUserPhone", phone);
        return query.listResult();
    }

}
